import { route, GET, POST } from 'awilix-express';

@route('/message')
export default class MessageApi {

  constructor({ messageService }) {
    this.messageService = messageService;
  }

  @route('/getMessageAll')
  @POST()
  async getMessageAll(req, res){
    const {
      mark
    } = req.body
    const { success, data: list, message } = await this.messageService.getMessageAll({mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/getMessagelist')
  @POST()
  async getMessagelist(req, res) {
    const { t_tell, mark, limit, offset } = req.body
    const { success, data: {count, rows}, message } = await this.messageService.getMessagelist({t_tell, mark, limit, offset});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, {count, rows});
  }

  @route('/addMessage')
  @POST()
  async addMessage(req, res) {
    const {
      t_tell,
      mark
     } = req.body;
    const { success, message } = await this.messageService.addMessage({
      t_tell,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/delMessage')
  @POST()
  async delMessage(req, res) {
    const { id, mark } = req.body;
    const { success, message } = await this.messageService.delMessage({id, mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  @route('/updateMessage')
  @POST()
  async updateMessage(req, res) {
    const {
      id,
      t_tell
    } = req.body;
    const { success, message } = await this.messageService.updateMessage({
      id,
      t_tell
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }
}